package controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import model.Student;
import model.User;

/**
 * Created by BaiYunfei on 2017/6/14.
 */
public class StudentController extends BaseController {

    public void list(){
        Page<Student> studentPage = Student.dao.paginate(getPageNumber(),getPageSize(),"SELECT *", "FROM Student");
        renderDataTable(studentPage);
    }

    @Before(Tx.class)
    public void save(){
        Student student = getModel(Student.class,"");
        if(Student.dao.findById(student.getStr("Student_ID")) != null){
            student.update();
        }
        
        
        User user = new User();
        user.setId(User.dao.generateId());
        user.setUsername(student.getStr("Teacher_ID"));
        user.setPassword(student.getStr("Teacher_ID"));
        user.save();
        
        student.set("ID", user.get("ID"));
        student.save();
        setAttr("status","success");
        renderJson();
    }
    
    public void remove(){
    	String id = getPara("id");
    	Student.dao.deleteById(id);
    	renderJson();
    }
    
    public void addPage(){
    	if(isParaExists("id")){
    		String id = getPara("id");
    		Student stu  = Student.dao.findById(id);
    		setAttr("student",stu);
    	}
        render("student-add.html");
    }

    public void index(){
        render("student-list.html");
    }

}
